#ifndef CAPTUREWIDGET_H
#define CAPTUREWIDGET_H

#pragma once
#include <QObject>
#include <QWidget>
#include <QGuiApplication>
#include <QApplication>
#include <QDesktopWidget>
#include <QScreen>
#include <QMouseEvent>
#include <QDebug>
#include <KWindowEffects>
#include <KWindowSystem>
#include "cutdown.h"
#include "showkeyController.h"
#include "clickanimatecontrol.h"
#include "selectionwidget.h"
#include "toolbar.h"
#include "global/global.h"
#include "statusbar.h"
#include "camerapage.h"

#include <QPointer>

class CaptureWidget : public QWidget
{
    Q_OBJECT
public:
    explicit CaptureWidget(const uint id = 0,
                           const QString &savePath = QString(), bool fullScreen = true,
                           QWidget *parent = nullptr);
    ~CaptureWidget();
    void slotShowKey(bool isShowKey);
    void slotCloseRec();

    void openCameraRecord(bool isShow);             //显示摄像头
    void resetCameraPos();
    void slotUpdateCameraDisplay(bool isShow);      //更新设置里的摄像头的显示

    void updateMicOption(bool isEnable);

signals:
    void captureExit(uint id);                          //退出捕获
    void sigStartFromToolbar();

protected:
    void paintEvent(QPaintEvent * e) override;
    void mousePressEvent(QMouseEvent * e) override;
    void mouseMoveEvent(QMouseEvent * e) override;
    void mouseReleaseEvent(QMouseEvent * e) override;
    void keyPressEvent(QKeyEvent * e) override;
    void resizeEvent(QResizeEvent * e) override;

private slots:
    void slotExitGui();                                     //退出并取消录制状态
    void slotCloseRecGui();                                 //关闭退出捕获窗口
    void slotHideGUI();
    void slotStartRec(int cutdown);
    void slotPauseRec();
    void slotContinueRec();
    void slotCancelRec();
    void slotOpenSettingWid();
    void slotCloseCamera();


private:
    void initSelection();
    void updateCursor();                                    //更新鼠标光标形状
    void initConnect();
    void updateCrosspixmap(QPoint e);                       //鼠标框选时的放大框

    void updateToolBarPos(const QRect &selection);          //更新工具栏的位置
    QRect intersectWithAreas(const QRect &rect);
    void updateBlockedSides();                              //确定选框周围的工具栏状态
    void ensureSelectionMinimunSize();
    void updateScreenRegions();                             //设置当前屏幕大小

    void updateSelectionBorder(QPainter &painter);          //绘制选框边框和手柄
    void updateSelectionControlPoint(QPainter &painter);    //绘制选框手柄
    void updateSizeLabelOption();                           //更新选框大小文字标签

    void updateCutDownPos(const QRect &selection);          //更新倒计时的位置
    void checkNeedShowCursor();

    void updateCameraPos(QPoint point);
    void updateCameraSize(QPoint point);

    uint m_id;                  //request的id
    int m_opacity;              //选框之外的不透明度
    bool isfirstPress;          //第一次按压
    bool isfirstRelease;        //第一次释放
    bool isMousePress;          //鼠标按压
    bool isMouseRelease;        //鼠标释放
    bool isHoverWidow;          //确定选框前的窗口抓取
    bool isMove;                //按压移动选框
    bool isSize;                //按压调节大小
    QPoint m_dragStartPoint;    //鼠标第一次按压的位置
    QPoint m_mousePos;          //鼠标移动后的位置

    SelectionWidget *m_selection;                   //选框
    SelectionWidget::SideType m_mouseOverHandle;    //鼠标在选框的位置

    ToolBar *m_toolBar;                             //工具栏
    CutDown *m_cutdown;                             //倒计时
    ShowkeyController *m_showKeyController;         //按键
    ClickAnimateControl *m_clickAnimateControl;     //点击效果和路径
    DesktopInfo m_info;

    //屏幕窗口信息
    QVector<QRect> areas;                           //存储所有的屏幕信息
    QVector<QRect> rects;                           //存储当前已打开窗口信息
    QHash<QRect, WId> mapRectToWindowId;            //窗口与id的映射

    //选框相关数据
    bool m_isFullscreen = false;                    //是否全屏
    bool m_isWindow = false;                        //是否窗口
    WId m_winId = 0;                                //应用录制时的窗口id
    QRect m_selectionRec;                           //选择窗口
    QString m_screen;                               //屏幕
    double m_scale = 1;                             //系统缩放率

    //更新工具栏位置
    int m_buttonBaseSize;
    const int toolbarSelectionMargin = 12;          //选框与工具栏的距离为12
    bool m_blockedRight;                            //选框周围的工具栏的阻塞状态
    bool m_blockedLeft;
    bool m_blockedBottom;
    bool m_blockedTop;
    bool m_allSidesBlocked;
    QRect m_RectSelection;                          //选框的备份
    QRegion m_screenRegions;
    QRect m_buttonCurScreen;                        //工具栏所在的屏幕
    QVector<QRect> allrect;                         // = areas 所有屏幕

    //绘制
    QColor m_uiColor;                               //主ui颜色
    QLabel *size_label;                             //选框大小标签

    //摄像头
    QPointer<CameraPage> m_camera;                  //摄像头
    bool isMoveCamera = false;
    bool isSizeCamera;                //按压调节大小
    CameraPage::SideType m_mouseOverHandleCamera;    //鼠标在摄像头的位置
    bool isSelectOut = false;
    QPushButton *m_button = nullptr;
    bool isDisplayCamerathis = true;    //本次不显示摄像头
};

#endif // CAPTUREWIDGET_H
